var table;
var layer;
var upload;
var form;
var laydate;
layui.use(['layer','table','upload','form','laydate'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;
    laydate=layui.laydate;

    //选择优惠券类型
    form.on("radio(status)", function () {
        if( this.value==2){ //满减券
            var coupon=document.getElementById("coupon");
            coupon.removeAttribute("hidden")
        }else if(this.value==3){//优惠券
            $("input[name=couponMoney]").val(0);

            var coupon=document.getElementById("coupon");
            coupon.setAttribute("hidden","hidden");
        }
    });



    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#createId").val(create.user_id);

    //开始时间
    laydate.render({
        elem: '#startDate',
        type: 'datetime',
        trigger:'click',
        done:function (value,date) {
            $("#startDate").val(value);
        }
    });

    //结束时间
    laydate.render({
        elem: '#endDate',
        type: 'datetime',
        trigger:'click',
        done:function (value,date) {
            $("#endDate").val(value);
        }
    });

    randomn();
    function randomn() {
        var str=parseInt((Math.random() + 1) * Math.pow(10,10-1));
        $("#couponCode").val(str);
    }

});

//返回
function go() {
    history.go(-1)
}

//刷新
function reload() {
    location.reload();
}

//添加优惠券
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:"/SyOperationController/addCoupon",
            type:"post",
            dataType:"json",
            traditional: true,//ajax传递数组必须添加属性
            data:data.field,
            success:function(data){
                if(data.code==0){
                    location.reload()
                }else{
                    layer.msg('添加失败');
                }
            }
        });
        return false;
    });
});

